from students.gju.DBUtil import Database
from students.studends.stuend import Student


class StudentDB:
    def __init__(self):
        host = '127.0.0.1'
        port = 3306
        user = 'root'
        passwd = '123456'
        db = 'students'
        self.dbobj,self.cursor=Database.openDB(host, port, user, passwd, db)


    def delete(self,sno):
        sql=f"delete from students where sno='{sno}'"
        result=Database.writeDB(sql,self.cursor)
        self.dbobj.commit()
        return result

    def add(self,student):
        sql = f"insert into students(sno,name,age,phone,address,sex) values('{student.sno}','{student.name}',{student.age},'{student.phone}','{student.address}','{student.sex}')"
        result=Database.writeDB(sql, self.cursor)
        self.dbobj.commit()
        return result

    def update(self,student):
        sql = f"update students set sno='{student.sno}',name='{student.name}',age={student.age},phone='{student.phone}',address='{student.address}',sex='{student.sex}'where id={student.id}"
        result=Database.writeDB(sql, self.cursor)
        self.dbobj.commit()
        return result

    def search(self,sno=None,name=None):
        if sno !=None:
            sql=f"select * from students where sno='{sno}'"
        if name != None:
            sql = f"select * from students where name='{name}'"
        result=Database.readALL(sql,self.cursor)
        return result





    def __del__(self):
        Database.closeDB(self.dbobj)

if __name__ == '__main__':
    s1=StudentDB()
    # s1.delete('none')
    # s2=Student()
    # s2.sno='s004'
    # s2.name='赵7'
    # s2.age=66
    # s2.sex='男'
    # s2.phone='24344555'
    # s2.address='china'
    # s1.add(s2)
    #
    # s2.id=7
    # s2.sno='s004'
    # s2.name='赵7'
    # s2.age=66
    # s2.sex='男'
    # s2.phone='24344555'
    # s2.address='china'
    # s1.update(s2)

    result=s1.search(sno='s003')
    print(result)
